#include "sharemem.hpp"

int main()
{

	int shmid = createshm();
	// log(Debug,"create shm done,shmid:%d",shmid);

	// sleep(5);

	Init init;
	int fd = open(FILE_PATH, O_RDONLY);
	if (fd < 0)
	{
		// perror("open:");
		log(Fatal, "error string:%s,error code:%d", strerror(errno), errno);
		exit(FIFO_OPEN_ERR);
	}

	char *shmaddr = (char *)shmat(shmid, nullptr, 0);
	// log(Debug,"attach shm done,addr:0x%x",shmaddr);

	// sleep(3);

	// int cnt = 10;
	while (1)
	{
		char c;
		int n = read(fd, &c, 1);
		if (n == 0)
			break;
		else if (n < 0)
			break;
		cout << "people say@ " << shmaddr << endl;
		struct shmid_ds ds;
		shmctl(shmid, IPC_STAT, &ds);

		cout << "shmid key:" << ds.shm_perm.__key << endl;
		cout << "shmid attach:" << ds.shm_nattch << endl;
		cout << "shmid size:" << ds.shm_segsz << endl;
	}

	shmdt(shmaddr);
	// log(Debug,"detach shm done,addr:0x%x",shmaddr);

	// sleep(3);
	shmctl(shmid,IPC_RMID,nullptr);
	// log(Debug,"destory shm done,addr:0x%x",shmaddr);

	// sleep(3);
	// log(Debug,"quit success");
	return 0;
}